/**
 * 试剂store
 */
import { makeAutoObservable } from "mobx";
import {
  fetchReagentList,
  fetchAddReagent,
  fetchAddUserReagent,
  fetchAllReagent,
  fetchDelReagent,
  fetchDelUserReagent,
  fetchEditReagent,
  fetchEditUserReagent,
  fetchImportReagent,
  fetchSearchReagent,
  fetchSearchUserReagent,
  fetchReagentInfoByCas,
  fetchImportCasReagent,
  fetchReagentInfoById,
  fetchUserReagentList
} from "../api/reagent";
import { ITReagent, ITBadResp, ITReagentTypeTitle } from "./interfaces";
import { checkResonse, ITResponseData } from "../api";
import { getReagentType, getUserInfo } from "../utils";

export interface ITReagentStore {
  // 化学试剂列表
  reagentList: ITReagent[];

  // 修改化学试剂列表
  changeReagentList: (data: ITReagent[]) => void;

  // 用户化学试剂列表
  userReagentList: ITReagent[];

  // 修改用户化学试剂列表
  changeUserReagentList: (data: ITReagent[]) => void;

  // 请求用户化学试剂
  getUserReagentList: (data: any) => Promise<ITBadResp | null>;

  // 请求化学试剂状态
  reagentListLoading: boolean;

  // 修改请求化学试剂状态
  changeReagentListLoading: (data: boolean) => void;

  // 请求化学试剂
  getReagentList: () => Promise<ITBadResp | null>;

  // 所有化学试剂
  allReagent: ITReagent[];

  // 修改所有化学试剂
  changeAllReagent: (data: any) => void;

  // 请求所有化学试剂状态
  allReagentListLoading: boolean;

  // 修改请求所有化学试剂
  changeAllReagentListLoading: (data: boolean) => void;

  // 请求所有化学试剂
  getAllReagetList: () => Promise<ITReagent[]>;

  // 当前页码
  page: number;

  // 请求数据条数
  pageSize: number;

  // 总数据条数
  totalPage: number;

  // 修改页码
  changePage: (data: number) => void;

  // 修改数据总数
  changeTotalPage: (data: number) => void;

  // 显示新增编辑弹窗
  showAddEditModal: boolean;

  // 修改显示新增编辑弹窗
  changeShowAddEditModal: (data: boolean) => void;

  // 新增编辑试剂请求状态
  addEditReagentLoading: boolean;

  // 修改新增编辑试剂请求状态
  changeAddEditReagentLoading: (data: boolean) => void;

  // 新增化学试剂
  addReagent: (data: any) => Promise<ITBadResp | null>;

  // 新增用户化学试剂
  addUserReagent: (data: any) => Promise<ITBadResp | null>;

  // 修改的化学试剂
  editingReagentData: ITReagent | null;

  // 编辑修改的化学试剂
  changeEditingReagentData: (data: ITReagent | null) => void;

  // 修改化学试剂
  editReagent: (data: any) => Promise<ITBadResp | null>;

  // 修改用户化学试剂
  editUserReagent: (data: any) => Promise<ITBadResp | null>;

  // 删除试剂请求状态
  delReagentLoading: boolean;

  // 修改删除试剂请求状态
  changeDelReagentLoading: (data: boolean) => void;

  // 删除化学试剂
  delReagent: (data: any) => Promise<ITBadResp | null>;

  // 删除用户化学试剂
  delUserReagent: (data: any) => Promise<ITBadResp | null>;


  // 是否显示导入数据弹窗
  showImportModal: boolean;

  // 修改是否显示导入数据弹窗
  changeShowImportModal: (data: boolean) => void;

  // 是否显示导出数据弹窗
  showExportModal: boolean;

  // 修改是否显示导出数据弹窗
  changeShowExportModal: (data: boolean) => void;

  // 导入化学试剂请求状态
  importLoading: boolean;

  // 修改导入化学试剂请求状态
  changeImportLoading: (data: boolean) => void;

  // 导入化学试剂请求
  ImportData: (data: any) => Promise<ITBadResp | null>;

  // 批量导入Casno数据
  ImportAllCasData: (data: any) => Promise<ITBadResp | null>;

  // 搜索列表
  searchList: ITReagent[] | null;

  // 修改搜索列表
  changeSearchList: (data: ITReagent[] | null) => void;

  // 搜索请求状态
  searchLoading: boolean;

  // 修改搜索请求状态
  changeSearchLoading: (data: boolean) => void;

  // 搜索化学试剂
  searchReagent: (data: any) => Promise<ITReagent[]>;

  // 搜索用户化学试剂
  searchUserReagent: (data: any) => Promise<ITReagent[]>;

  // 根据id搜索化学试剂
  searchReagentById: (data: any) => Promise<ITReagent | null>;

  // 返回试剂单位ml g 毫升 克
  getReagentUnit: (id: string, lang: any) => string;

  // 返回试剂标题 质量 容量
  getReagentTitle: (id: string, lang: any) => ITReagentTypeTitle;

  getEnReagentUnit: (id: string) => string;

  // 根据id获取试剂详情
  getReagentInfoById: (id: string) => ITReagent | null;

  // 根据casno查询试剂信息
  getReagentInfoByCasno: (data: any) => Promise<ITBadResp | null>;
}

class ReagentStore {
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true });
  }

  // 化学试剂列表
  reagentList: ITReagent[] = [];

  // 修改化学试剂列表
  changeReagentList = (data: ITReagent[]) => (this.reagentList = data);

  // 用户化学试剂列表
  userReagentList: ITReagent[] = [];

  // 修改用户化学试剂列表
  changeUserReagentList = (data: ITReagent[]) => (this.userReagentList = data);

  // 请求化学试剂状态
  reagentListLoading = false;

  // 修改请求化学试剂状态
  changeReagentListLoading = (data: boolean) =>
    (this.reagentListLoading = data);

  // 请求化学试剂
  getReagentList = async (): Promise<ITBadResp | any> => {
    this.changeReagentListLoading(true);
    const res = await fetchReagentList({
      page: this.page,
      pageSize: this.pageSize,
    });
    this.changeReagentListLoading(false);
    if (checkResonse(res)) {
      const result: ITResponseData = res.data;
      const {
        data: { list, count },
      } = result;
      list.forEach((item: ITReagent, index: number) => (item.key = index));
      this.changeReagentList(list);
      this.changeTotalPage(count);
    }
  };

  // 用户化学试剂列表
  getUserReagentList = async (data: any): Promise<ITBadResp | any> => {
    this.changeReagentListLoading(true);
    const res = await fetchUserReagentList({
      ...data,
      page: this.page,
      pageSize: this.pageSize,
    });
    this.changeReagentListLoading(false);
    if (checkResonse(res)) {
      const result: ITResponseData = res.data;
      const {
        data: { list, count },
      } = result;
      list.forEach((item: ITReagent, index: number) => (item.key = index));
      this.changeUserReagentList(list);
      this.changeTotalPage(count);
    }
  };

  // 当前页码
  page = 1;

  // 请求数据条数
  pageSize = 10;

  // 总数据条数
  totalPage = 1;

  // 修改页码
  changePage = (data: number) => (this.page = data);

  // 修改数据总数
  changeTotalPage = (data: number) => (this.totalPage = data);

  // 显示新增编辑弹窗
  showAddEditModal = false;

  // 修改显示新增编辑弹窗
  changeShowAddEditModal = (data: boolean) => (this.showAddEditModal = data);

  // 新增编辑试剂请求状态
  addEditReagentLoading = false;

  // 修改新增编辑试剂请求状态
  changeAddEditReagentLoading = (data: boolean) =>
    (this.addEditReagentLoading = data);

  // 新增化学试剂
  addReagent = async (data: any): Promise<ITBadResp | any> => {
    this.changeAddEditReagentLoading(true);
    const res = await fetchAddReagent(data);
    this.changeAddEditReagentLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  addUserReagent = async (data: any): Promise<ITBadResp | any> => {
    this.changeAddEditReagentLoading(true);
    const res = await fetchAddUserReagent(data);
    this.changeAddEditReagentLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 修改的化学试剂
  editingReagentData: ITReagent | null = null;

  // 编辑修改的化学试剂
  changeEditingReagentData = (data: ITReagent | null) =>
    (this.editingReagentData = data);

  // 修改化学试剂
  editReagent = async (data: any): Promise<ITBadResp | null> => {
    this.changeAddEditReagentLoading(true);
    const res = await fetchEditReagent(data);
    this.changeAddEditReagentLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  editUserReagent = async (data: any): Promise<ITBadResp | null> => {
    this.changeAddEditReagentLoading(true);
    const res = await fetchEditUserReagent(data);
    this.changeAddEditReagentLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  

  // 删除试剂请求状态
  delReagentLoading = false;

  // 修改删除试剂请求状态
  changeDelReagentLoading = (data: boolean) => (this.delReagentLoading = data);

  // 删除化学试剂
  delReagent = async (data: any): Promise<ITBadResp | null> => {
    this.changeDelReagentLoading(true);
    const res = await fetchDelReagent(data);
    this.changeDelReagentLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  delUserReagent = async (data: any): Promise<ITBadResp | null> => {
    this.changeDelReagentLoading(true);
    const res = await fetchDelUserReagent(data);
    this.changeDelReagentLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 是否显示导入数据弹窗
  showImportModal = false;

  // 修改是否显示导入数据弹窗
  changeShowImportModal = (data: boolean) => (this.showImportModal = data);

  // 是否显示导出数据弹窗
  showExportModal = false;

  // 修改是否显示导出数据弹窗
  changeShowExportModal = (data: boolean) => (this.showExportModal = data);

  // 所有化学试剂
  allReagent: ITReagent[] = [];

  // 修改所有化学试剂
  changeAllReagent = (data: any) => (this.allReagent = data);

  // 请求所有化学试剂状态
  allReagentListLoading = false;

  // 修改请求所有化学试剂
  changeAllReagentListLoading = (data: boolean) =>
    (this.allReagentListLoading = data);

  // 请求所有化学试剂
  getAllReagetList = async (): Promise<ITReagent[]> => {
    let reagents: ITReagent[] = [];
    this.changeAllReagentListLoading(true);
    const userInfo = getUserInfo()
    const userRes = await fetchUserReagentList({
        user: userInfo.id,
        pageNum: 1,
        pageSize: 10000
    });

    let userReagents = [];
    if(checkResonse(userRes)){
        userReagents = userRes.data.data.list || [];
    }

    const res = await fetchAllReagent();
    this.changeAllReagentListLoading(false);
    if (checkResonse(res)) {
      const { data } = res.data;
      reagents = [...userReagents ,...data];
      this.changeAllReagent(reagents);
    }
    return reagents;
  };

  // 导入化学试剂请求状态
  importLoading = false;

  // 修改导入化学试剂请求状态
  changeImportLoading = (data: boolean) => (this.importLoading = data);

  // 导入化学试剂请求
  ImportData = async (data: any): Promise<ITBadResp | null> => {
    this.changeImportLoading(true);
    const res = await fetchImportReagent(data);
    this.changeImportLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 批量导入Casno化学试剂请求
  ImportAllCasData = async (data: any): Promise<ITBadResp | null> => {
    const res = await fetchImportCasReagent(data);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  // 搜索列表
  searchList: ITReagent[] | null = null;

  // 修改搜索列表
  changeSearchList = (data: ITReagent[] | null) => (this.searchList = data);

  // 搜索请求状态
  searchLoading = false;

  // 修改搜索请求状态
  changeSearchLoading = (data: boolean) => (this.searchLoading = data);

  // 搜索化学试剂
  searchReagent = async (data: any): Promise<ITReagent[]> => {
    this.changeSearchLoading(true);
    const res = await fetchSearchReagent(data);
    this.changeSearchLoading(false);
    if (checkResonse(res)) {
      const { data } = res.data;
      data.forEach((item: ITReagent, index: number) => {
        item.key = index;
      });
      return data;
    }
    return [];
  };

  searchUserReagent = async (data: any): Promise<ITReagent[]> => {
    this.changeSearchLoading(true);
    const res = await fetchSearchUserReagent(data);
    this.changeSearchLoading(false);
    if (checkResonse(res)) {
      const { data } = res.data;
      data.forEach((item: ITReagent, index: number) => {
        item.key = index;
      });
      return data;
    }
    return [];
  };

  // 根据id搜索化学试剂
  searchReagentById = async (data: any): Promise<ITReagent | null> => {
    const res = await fetchReagentInfoById(data);
    if (checkResonse(res)) {
      const { data } = res.data;
      return data;
    }
    return null;
  };

  // 返回试剂单位ml g 毫升 克
  getReagentUnit = (id: string, lang: any) => {
    let reagentInfo = null;
    let unit = "";
    this.allReagent.some((item) => {
      if (item.id === id) {
        reagentInfo = item;
        return true;
      }
      return false;
    });
    if (reagentInfo) {
      const { type } = reagentInfo;
      const reagentType = getReagentType(type);
      if (reagentType === 2) {
        unit = lang["langmg"];
      } else {
        unit = lang["langmlunit"];
      }
    }
    return unit;
  };

  // 返回试剂标题 质量 容量
  getReagentTitle = (id: string, lang: any): ITReagentTypeTitle => {
    let reagentInfo = null;
    let reagentTitle: string = "";
    let reagentType: number = 0;
    this.allReagent.some((item) => {
      if (item.id === id) {
        reagentInfo = item;
        return true;
      }
      return false;
    });
    if (reagentInfo) {
      const { type } = reagentInfo;
      const tempReagentType = getReagentType(type);
      if (tempReagentType === 2) {
        reagentTitle = lang["langweight"];
        reagentType = 2;
      } else {
        reagentTitle = lang["langvolume"];
        reagentType = 1;
      }
    }
    return {
      type: reagentType,
      title: reagentTitle,
    };
  };

  getEnReagentUnit = (id: string) => {
    let reagentInfo = null;
    let unit = "";
    this.allReagent.some((item) => {
      if (item.id === id) {
        reagentInfo = item;
        return true;
      }
      return false;
    });
    if (reagentInfo) {
      const { type } = reagentInfo;
      const reagentType = getReagentType(type);
      if (reagentType === 2) {
        unit = "mg";
      } else {
        unit = "ml";
      }
    }
    return unit;
  };

  // 根据id获取试剂详情
  getReagentInfoById = (id: string): ITReagent | null => {
    let tempInfo: ITReagent | null = null;
    if (this.allReagent && this.allReagent.length) {
      this.allReagent.some((item) => {
        if (item.id === id) {
          tempInfo = item;
          return true;
        }
        return false;
      });
    }
    return tempInfo;
  };

  getReagentInfoByCasno = async (data: string): Promise<ITBadResp | null> => {
    const res = await fetchReagentInfoByCas(data);
    if (checkResonse(res)) {
      return res.data.data;
    }
    return null;
  };
}

const reagentStore: ITReagentStore = new ReagentStore();
export default reagentStore;
